package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import model.BattleReport;
	import model.macro.Armor;
	import model.macro.Building;
	import model.macro.Player;
	import model.macro.RangedWeapon;
	import model.macro.Territory;
	import model.macro.Unit;
	import model.macro.Weapon;
	import model.macro.World;
	import view.FrameRateCounter;
	import view.macro.MapSprite;
	
	/**
	 * Runs main menu options and loads/saves games
	 * 
	 */
	public class GameLoader extends Sprite
	{
		private var counter:FrameRateCounter;
		
		// To be removed
		private var current_world:World;
		private var current_map:MapSprite;
		
		// Pseudo Code
		// private var main_menu:MainMenu;
		// private var current_game:GameFile;
		
		public function GameLoader() 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			counter = new FrameRateCounter();
			counter.x = 5;
			counter.y = stage.stageHeight - counter.height;
			addChild(counter);
			
			// To be removed
			readGameFromFile();
			runCurrentWorld();
			
			// Pseudo code
			// main_menu = new MainMenu();
			// main_menu.addEventListener(... , readGameFromFile);
			// main_menu.addEventListener(... , writeCurrentGameToFile);
			// main_menu.addEventListener(... , runCurrentWorld);
			// addChild(main_menu);
		}
		
		public function readGameFromFile():void
		{
			tempWorldGen();
		}
		
		public function writeCurrentGameToFile():void
		{
			
		}
		
		public function runCurrentWorld():void
		{
			var game:Game = new Game(current_world, current_map, new Vector.<BattleReport>());
			// var game:Game = new Game(current_game.world, current_game.map, current_game.battle_history);
			
			addChild(game); 
		}
		
		// To be removed
		public function tempWorldGen():void
		{
			// players
			var players:Vector.<Player> = new Vector.<Player>();
			players.push(new Player("Player 1", 0x0000FF));
			players.push(new Player("Player 2", 0xFF0000));
			
			players[0].hostilePlayers.push(players[1]);
			players[1].hostilePlayers.push(players[0]);
			
			var territories:Vector.<Territory> = new Vector.<Territory>();
			
			// TODO: load territories
			for (var i:int = 0; i < 12; i++)
			{
				territories.push(new Territory("Territory " + i));
			}
			
			territories[0].owner = players[0];
			territories[1].owner = players[1];
			
			current_world = new World(territories, players, "Test", players[0]);
			
			// mark adjacent territories
			var adjacent:Vector.<Point> = new Vector.<Point>();
			adjacent.push(new Point(4, 9));
			adjacent.push(new Point(2, 6));
			adjacent.push(new Point(0, 1));
			adjacent.push(new Point(0, 4));
			adjacent.push(new Point(0, 5));
			adjacent.push(new Point(5, 6));
			adjacent.push(new Point(6, 7));
			adjacent.push(new Point(7, 11));
			for each (var pair:Point in adjacent)
			{
				current_world.territories[pair.x].adjacent_territories.push(current_world.territories[pair.y]);
				current_world.territories[pair.y].adjacent_territories.push(current_world.territories[pair.x]);
			}
			
			current_world.territories[0].buildings.push(Building.BARRACKS);
			for each (var territory:Territory in current_world.territories)
			{
				territory.addBuilding(Building.BLACKSMITH);
				territory.addBuilding(Building.MARKET);
				territory.addBuilding(Building.SEIGE_WORKSHOP);
			}
			current_world.territories[0].units.push(new Unit(current_world.players[0], Weapon.SWORD, Armor.CHAINMAIL));
			current_world.territories[0].units.push(new Unit(current_world.players[0], Weapon.SWORD, Armor.CLOTH));
			current_world.territories[0].units.push(new Unit(current_world.players[0], Weapon.HALBERD, Armor.FULLPLATE));
			
			current_world.territories[1].units.push(new Unit(current_world.players[1], Weapon.HALBERD, Armor.CHAINMAIL));
			current_world.territories[1].units.push(new Unit(current_world.players[1], RangedWeapon.LONGBOW, Armor.CHAINMAIL));
			current_world.territories[1].units.push(new Unit(current_world.players[1], RangedWeapon.LONGBOW, Armor.CHAINMAIL));
			current_world.territories[1].units.push(new Unit(current_world.players[1], RangedWeapon.LONGBOW, Armor.CHAINMAIL));
			current_world.territories[1].units.push(new Unit(current_world.players[1], RangedWeapon.LONGBOW, Armor.CHAINMAIL));
			
			current_map = new MapSprite(current_world.territories, current_world);
		}
		
	}

}